package com.ytheima;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;

public class MapDemo {
        public static void main(String[] args) {
            test4();
        }
        public static void test4() {
            Map<String, Double> map = new HashMap<>();
            map.put("张三", 90.0);
            map.put("李四", 80.0);
            map.put("王五", 100.0);
            map.put("赵六", 60.0);
//            map.forEach(new BiConsumer<String, Double>() {
//                @Override
//                public void accept(String s, Double aDouble) {
//                    System.out.println(s + "---" + aDouble);
//                }
//            });
            map.forEach((key, value) -> System.out.println(key + "---" + value));//上式的Lambda表达式
        }

        public static void test1() {
            Map<String, Double> map = new HashMap<>();
            map.put("张三", 90.0);
            map.put("李四", 80.0);
            map.put("王五", 100.0);
            map.put("赵六", 60.0);
            //将Map中的所有键取出来放到set集合中
            Set<String> keySet = map.keySet();
            for (String key : keySet) {
                Double value = map.get(key);
                System.out.println(key + "---" + value);
            }
        }
        public static void test3() {
            Map<String, Double> map = new HashMap<>();
            map.put("张三", 90.0);
            map.put("李四", 80.0);
            map.put("王五", 100.0);
            map.put("赵六", 60.0);
            //将Map集合转换为Set集合（将map中的key-value对封装成entry对象，并返回）
           Set<Map.Entry<String, Double>> entrySet = map.entrySet();
           //遍历set集合
            for (Map.Entry<String, Double> entry : entrySet) {
                //取出key
                String key = entry.getKey();
                //根据entry取出key和value
                Double value = entry.getValue();
                System.out.println(key + "---" + value);
            }
        }

        public static void test2() {
            Map<String, Double> map = new HashMap<>();
            map.put("张三", 90.0);
            map.put("李四", 80.0);
            map.put("王五", 100.0);
            map.put("赵六", 60.0);
            System.out.println(map);

            map.remove("张三");
            System.out.println(map);
//        map.clear();
//        System.out.println(map);

            boolean b1 = map.containsKey("李四");
            System.out.println("b1 = " + b1);
            boolean b2 = map.containsKey("80");
            System.out.println("b2 = " + b2);

            boolean empty = map.isEmpty();
            System.out.println("empty = " + empty);
            int size = map.size();
            System.out.println("size = " + size);

        }

}
